﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Security.AccessControl;

namespace BugSnapper
{
    public static class AppGlobal
    {
        private static bool _compress = false;
        public static bool Compress
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["compress"]))
                    {
                        _compress = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["compress"]) == "0" ? false : true;
                    }
                    else
                    {
                        _compress = false;
                    }
                }
                catch
                {
                    _compress = false;
                }
                return _compress;
            }
            set
            {
                try
                {
                    _compress = value;
                    if (value)
                    {
                        UpdateKey("compress", "1");
                    }
                    else
                    {
                        UpdateKey("compress", "0");
                    }
                }
                catch { }
            }
        }
        private static bool _keyLess = false;
        public static bool KeyLess
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["keyless"]))
                    {
                        _keyLess = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["keyless"]) == "0" ? false : true;
                    }
                    else
                    {
                        _keyLess = false;
                    }
                }
                catch
                {
                    _keyLess = false;
                }
                return _keyLess;
            }
            set
            {
                try
                {
                    _keyLess = value;
                    if (value)
                    {
                        UpdateKey("keyless", "1");
                    }
                    else
                    {
                        UpdateKey("keyless", "0");
                    }
                }
                catch { }
            }
        }
        public enum SaveDocumentFormat
        {
            Word = 0,
            PDF = 1,
        }

        private static SaveDocumentFormat _saveDocumentAs = SaveDocumentFormat.Word;
        public static SaveDocumentFormat SaveDocumentAs
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["savedocumentas"]))
                    {
                        switch (Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["savedocumentas"]))
                        {
                            case "0":
                                {
                                    _saveDocumentAs = SaveDocumentFormat.Word;
                                    break;
                                }
                            case "1":
                                {
                                    _saveDocumentAs = SaveDocumentFormat.PDF;
                                    break;
                                }
                            default:
                                {
                                    _saveDocumentAs = SaveDocumentFormat.Word;
                                    break;
                                }
                        }
                    }
                    else
                    {
                        _saveDocumentAs = SaveDocumentFormat.Word;
                    }
                }
                catch
                {
                    _saveDocumentAs = SaveDocumentFormat.Word;
                }
                return _saveDocumentAs;
            }
            set
            {
                try
                {
                    _saveDocumentAs = value;
                    switch (value)
                    {
                        case SaveDocumentFormat.Word:
                            {
                                UpdateKey("savedocumentas", "0");
                                break;
                            }
                        case SaveDocumentFormat.PDF:
                            {
                                UpdateKey("savedocumentas", "1");
                                break;
                            }
                        default:
                            {
                                UpdateKey("savedocumentas", "0");
                                break;
                            }
                    }
                }
                catch { }
            }
        }


        private static bool _fittopage = false;
        public static bool FitToPage
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["fittopage"]))
                    {
                        _fittopage = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["fittopage"]) == "0" ? false : true;
                    }
                    else
                    {
                        _fittopage = false;
                    }
                }
                catch
                {
                    _fittopage = false;
                }
                return _fittopage;
            }
            set
            {
                try
                {
                    _fittopage = value;
                    if (value)
                    {
                        UpdateKey("fittopage", "1");
                    }
                    else
                    {
                        UpdateKey("fittopage", "0");
                    }
                }
                catch { }
            }
        }

        private static bool _dateTimeAsWaterMark = false;
        public static bool DateTimeAsWaterMark
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["datetimeaswatermark"]))
                    {
                        _dateTimeAsWaterMark = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["datetimeaswatermark"]) == "0" ? false : true;
                    }
                    else
                    {
                        _dateTimeAsWaterMark = false;
                    }
                }
                catch
                {
                    _dateTimeAsWaterMark = false;
                }
                return _dateTimeAsWaterMark;
            }
            set
            {
                try
                {
                    _dateTimeAsWaterMark = value;
                    if (value)
                    {
                        UpdateKey("datetimeaswatermark", "1");
                    }
                    else
                    {
                        UpdateKey("datetimeaswatermark", "0");
                    }
                }
                catch { }
            }
        }

        private static string _RootFolderPath = string.Empty;
        public static string RootFolderPath
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["path"]))
                    {
                        _RootFolderPath = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["path"]);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(_RootFolderPath))
                        {
                            _RootFolderPath = Convert.ToString(System.Environment.CurrentDirectory) + @"\tmpImages\";
                        }
                    }
                }
                catch
                {
                    _RootFolderPath = Convert.ToString(System.Environment.CurrentDirectory) + @"\tmpImages\";
                }
                return _RootFolderPath;
            }
            set
            {
                try
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        _RootFolderPath = value;
                        UpdateKey("path", _RootFolderPath);
                    }
                    else
                    {
                        _RootFolderPath = Convert.ToString(System.Environment.CurrentDirectory) + @"\tmpImages\";
                        UpdateKey("path", _RootFolderPath);
                    }
                }
                catch { }
            }
        }
        private static string _videoRootFolderPath = string.Empty;
        public static string VideoRootFolderPath
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["videopath"]))
                    {
                        _videoRootFolderPath = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["videopath"]);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(_videoRootFolderPath))
                        {
                            _videoRootFolderPath = Convert.ToString(System.Environment.CurrentDirectory) + @"\tmpVideos\";
                        }
                    }
                }
                catch
                {
                    _videoRootFolderPath = Convert.ToString(System.Environment.CurrentDirectory) + @"\tmpVideos\";
                }
                return _videoRootFolderPath;
            }
            set
            {
                try
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        _videoRootFolderPath = value;
                        UpdateKey("videopath", _videoRootFolderPath);
                    }
                    else
                    {
                        _videoRootFolderPath = Convert.ToString(System.Environment.CurrentDirectory) + @"\tmpVideos\";
                        UpdateKey("videopath", _videoRootFolderPath);
                    }
                }
                catch { }
            }
        }
        private static string _videoAuthorEmail = string.Empty;
        public static string VideoAuthorEmail
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["videoauthoremail"]))
                    {
                        _videoAuthorEmail = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["videoauthoremail"]);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(_videoAuthorEmail))
                        {
                            _videoAuthorEmail = string.Empty;
                        }
                    }
                }
                catch
                {
                    _videoAuthorEmail = string.Empty;
                }
                return _videoAuthorEmail;
            }
            set
            {
                try
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        _videoAuthorEmail = value;
                        UpdateKey("videoauthoremail", _videoAuthorEmail);
                    }
                    else
                    {
                        _videoAuthorEmail = string.Empty;
                        UpdateKey("videoauthoremail", _videoAuthorEmail);
                    }
                }
                catch { }
            }
        }
        private static string _videoAuthorWebsiteURL = string.Empty;
        public static string VideoAuthorWebsiteURL
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["videoauthorwebsiteurl"]))
                    {
                        _videoAuthorWebsiteURL = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["videoauthorwebsiteurl"]);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(_videoAuthorWebsiteURL))
                        {
                            _videoAuthorWebsiteURL = string.Empty;
                        }
                    }
                }
                catch
                {
                    _videoAuthorWebsiteURL = string.Empty;
                }
                return _videoAuthorWebsiteURL;
            }
            set
            {
                try
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        _videoAuthorWebsiteURL = value;
                        UpdateKey("videoauthorwebsiteurl", _videoAuthorWebsiteURL);
                    }
                    else
                    {
                        _videoAuthorWebsiteURL = string.Empty;
                        UpdateKey("videoauthorwebsiteurl", _videoAuthorWebsiteURL);
                    }
                }
                catch { }
            }
        }
        private static string _videoTitle = string.Empty;
        public static string VideoTitle
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["videotitle"]))
                    {
                        _videoTitle = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["videotitle"]);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(_videoTitle))
                        {
                            _videoTitle = string.Empty;
                        }
                    }
                }
                catch
                {
                    _videoTitle = string.Empty;
                }
                return _videoTitle;
            }
            set
            {
                try
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        _videoTitle = value;
                        UpdateKey("videotitle", _videoTitle);
                    }
                    else
                    {
                        _videoTitle = string.Empty;
                        UpdateKey("videotitle", _videoTitle);
                    }
                }
                catch { }
            }
        }
        private static string _videoRating = string.Empty;
        public static string VideoRating
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["videorating"]))
                    {
                        _videoRating = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["videorating"]);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(_videoRating))
                        {
                            _videoRating = string.Empty;
                        }
                    }
                }
                catch
                {
                    _videoRating = string.Empty;
                }
                return _videoRating;
            }
            set
            {
                try
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        _videoRating = value;
                        UpdateKey("videorating", _videoRating);
                    }
                    else
                    {
                        _videoRating = string.Empty;
                        UpdateKey("videorating", _videoRating);
                    }
                }
                catch { }
            }
        }
        private static string _videoDescription = string.Empty;
        public static string VideoDescription
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["videodescription"]))
                    {
                        _videoDescription = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["videodescription"]);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(_videoDescription))
                        {
                            _videoDescription = string.Empty;
                        }
                    }
                }
                catch
                {
                    _videoDescription = string.Empty;
                }
                return _videoDescription;
            }
            set
            {
                try
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        _videoDescription = value;
                        UpdateKey("videodescription", _videoDescription);
                    }
                    else
                    {
                        _videoDescription = string.Empty;
                        UpdateKey("videodescription", _videoDescription);
                    }
                }
                catch { }
            }
        }
        private static string _videoCopyright = string.Empty;
        public static string VideoCopyright
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["videocopyright"]))
                    {
                        _videoCopyright = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["videocopyright"]);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(_videoCopyright))
                        {
                            _videoCopyright = string.Empty;
                        }
                    }
                }
                catch
                {
                    _videoCopyright = string.Empty;
                }
                return _videoCopyright;
            }
            set
            {
                try
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        _videoCopyright = value;
                        UpdateKey("videocopyright", _videoCopyright);
                    }
                    else
                    {
                        _videoCopyright = string.Empty;
                        UpdateKey("videocopyright", _videoCopyright);
                    }
                }
                catch { }
            }
        }
        private static string _videoAuthor = string.Empty;
        public static string VideoAuthor
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["videoauthor"]))
                    {
                        _videoAuthor = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["videoauthor"]);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(_videoAuthor))
                        {
                            _videoAuthor = string.Empty;
                        }
                    }
                }
                catch
                {
                    _videoAuthor = string.Empty;
                }
                return _videoAuthor;
            }
            set
            {
                try
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        _videoAuthor = value;
                        UpdateKey("videoauthor", _videoAuthor);
                    }
                    else
                    {
                        _videoAuthor = string.Empty;
                        UpdateKey("videoauthor", _videoAuthor);
                    }
                }
                catch { }
            }
        }
        private static string _waterMarkText = string.Empty;
        public static string WaterMarkText
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["watermarktext"]))
                    {
                        _waterMarkText = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["watermarktext"]);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(_waterMarkText))
                        {
                            _waterMarkText = string.Empty;
                        }
                    }
                }
                catch
                {
                    _waterMarkText = string.Empty;
                }
                return _waterMarkText;
            }
            set
            {
                try
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        _waterMarkText = value;
                        UpdateKey("watermarktext", _waterMarkText);
                    }
                    else
                    {
                        _waterMarkText=string.Empty;
                        UpdateKey("watermarktext", _waterMarkText);
                    }
                }
                catch { }
            }
        }

        public enum ImageFormats
        {
            BMP = 0,
            JPEG = 1,
            GIF = 2,
            TIFF = 3,
            PNG = 4,
        }

        private static ImageFormats _saveAsImageFormat = ImageFormats.BMP;
        public static ImageFormats SaveAsImageFormat
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["saveasimageformat"]))
                    {
                        switch (Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["saveasimageformat"]))
                        {
                            case "0":
                                {
                                    _saveAsImageFormat = ImageFormats.BMP;
                                    break;
                                }
                            case "1":
                                {
                                    _saveAsImageFormat = ImageFormats.JPEG;
                                    break;
                                }
                            case "2":
                                {
                                    _saveAsImageFormat = ImageFormats.GIF;
                                    break;
                                }
                            case "3":
                                {
                                    _saveAsImageFormat = ImageFormats.TIFF;
                                    break;
                                }
                            case "4":
                                {
                                    _saveAsImageFormat = ImageFormats.PNG;
                                    break;
                                }
                            default:
                                {
                                    _saveAsImageFormat = ImageFormats.BMP;
                                    break;
                                }
                        }
                    }
                    else
                    {
                        _saveAsImageFormat = ImageFormats.BMP;
                    }
                }
                catch
                {
                    _saveAsImageFormat = ImageFormats.BMP;
                }
                return _saveAsImageFormat;
            }
            set
            {
                try
                {
                    _saveAsImageFormat = value;
                    switch (value)
                    {
                        case ImageFormats.BMP:
                            {
                                UpdateKey("saveasimageformat", "0");
                                break;
                            }
                        case ImageFormats.JPEG:
                            {
                                UpdateKey("saveasimageformat", "1");
                                break;
                            }
                        case ImageFormats.GIF:
                            {
                                UpdateKey("saveasimageformat", "2");
                                break;
                            }
                        case ImageFormats.TIFF:
                            {
                                UpdateKey("saveasimageformat", "3");
                                break;
                            }
                        case ImageFormats.PNG:
                            {
                                UpdateKey("saveasimageformat", "4");
                                break;
                            }

                        default:
                            {
                                UpdateKey("saveasimageformat", "0");
                                break;
                            }
                    }
                }
                catch { }
            }
        }

        private static int _timerPeriod = 0;
        public static int TimerPeriod
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["timerperiod"]))
                    {
                        int tempValue = 0;
                        _timerPeriod = int.TryParse(System.Configuration.ConfigurationManager.AppSettings["timerperiod"], out tempValue) ? tempValue * 1000 : tempValue * 1000;
                        _timerPeriod = (_timerPeriod < 1000) && (_timerPeriod > 0) ? 1000 : _timerPeriod;
                    }
                    else
                    {
                        _timerPeriod = 0;
                    }
                }
                catch
                {
                    _timerPeriod = 0;
                }
                return _timerPeriod;
            }
            set
            {
                try
                {

                    _timerPeriod = value;
                    UpdateKey("timerperiod", Convert.ToInt32((_timerPeriod / 1000)).ToString());
                }
                catch { }
            }
        }

        private static Logger Logger = new Logger();

        private static void UpdateKey(string strKey, string newValue)
        {
            try
            {
                if (!KeyExists(strKey))
                {
                    throw new ArgumentNullException("Key", "<" + strKey + "> does not exist in the configuration. Update failed.");
                }
                XmlDocument xmlDoc = new XmlDocument();
                DirectoryInfo dr = new DirectoryInfo(Application.StartupPath);
                string fileName = string.Empty;
                foreach (var item in dr.GetFiles())
                {
                    if (Path.GetExtension(item.FullName).ToLower() == ".config")
                    {
                        SetRule(item.FullName, "Everyone", FileSystemRights.Write, AccessControlType.Allow);
                        fileName = item.FullName;
                        xmlDoc.Load(item.FullName);
                        XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings");
                        // Attempt to locate the requested setting.

                        foreach (XmlNode childNode in appSettingsNode)
                        {
                            if (childNode.Attributes["key"].Value == strKey)
                            {
                                childNode.Attributes["value"].Value = newValue;
                            }
                        }
                        xmlDoc.Save(item.FullName);
                        System.Configuration.ConfigurationManager.RefreshSection("appSettings");
                        xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
                        System.Configuration.ConfigurationManager.RefreshSection("appSettings");
                    }
                }
                System.Configuration.ConfigurationManager.RefreshSection("appSettings");
            }
            catch (Exception ex)
            {
                Logger.ProcessError(ex);
            }
        }

        private static void AddRule(string filePath, string account, FileSystemRights rights, AccessControlType controlType)
        {
            FileSecurity fSecurity = File.GetAccessControl(filePath);
            fSecurity.AddAccessRule(new FileSystemAccessRule(account, rights, controlType));
            File.SetAccessControl(filePath, fSecurity);
        }

        private static void SetRule(string filePath, string account, FileSystemRights rights, AccessControlType controlType)
        {
            FileSecurity fSecurity = File.GetAccessControl(filePath);
            fSecurity.ResetAccessRule(new FileSystemAccessRule(account, rights, controlType));
            File.SetAccessControl(filePath, fSecurity);
        }

        private static bool KeyExists(string strKey)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
                XmlNode appSettingsNode =
                  xmlDoc.SelectSingleNode("configuration/appSettings");
                // Attempt to locate the requested setting.

                foreach (XmlNode childNode in appSettingsNode)
                {
                    if (childNode.Attributes["key"].Value == strKey)
                        return true;
                }
            }
            catch (Exception ex)
            {
                Logger.ProcessError(ex);
            }
            return false;
        }

        private static string _LineWidth = string.Empty;
        public static string LineWidth
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["linewidth"]))
                    {
                        _LineWidth = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["linewidth"]);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(_LineWidth))
                        {
                            _LineWidth = "5";
                        }
                    }
                }
                catch
                {
                    _LineWidth = "5";
                }
                return _LineWidth;
            }
            set
            {
                try
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        _LineWidth = value;
                        UpdateKey("linewidth", _LineWidth);
                    }
                }
                catch { }
            }
        }
    }
}
